# Contributor: Konstantin Kulikov <k.kulikov2@gmail.com>
# Maintainer: Konstantin Kulikov <k.kulikov2@gmail.com>
pkgname=victoria-metrics
pkgver=1.105.0
pkgrel=0
provides="victoriametrics=$pkgver-r$pkgrel"
pkgdesc="Fast, cost-effective and scalable time series database"
url="https://github.com/VictoriaMetrics/VictoriaMetrics"
# Test failures on other archs, FP precision mostly.
arch="x86_64 aarch64"
license="Apache-2.0"
makedepends="go"
subpackages="$pkgname-openrc $pkgname-tools:_tools $pkgname-backup-tools:_backup_tools"
install="$pkgname.pre-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/VictoriaMetrics/VictoriaMetrics/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	"
builddir="$srcdir/VictoriaMetrics-$pkgver"

# Depends on cgo zstd package that ships prebuilt object files built against glibc.
# It is possible to rebuild it against musl, but
# pure go zstd lib works well enough and is not written in C.
export CGO_ENABLED=0

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	local ldflags="-X github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo.Version=v$pkgver-r$pkgrel"
	local bin
	for bin in victoria-metrics vmagent vmalert vmauth vmctl vmrestore vmbackup; do
		go build -ldflags="$ldflags" -o bin/$bin ./app/$bin &
	done
	wait
}

check() {
	# TestStorageAddRowsConcurrent takes 140sec on x86_64 and times out.
	# TestConfigReload flakes in CI a lot.
	go test -short -parallel 4 -skip '^TestStorageAddRowsConcurrent$|^TestConfigReload$' ./...
}

package() {
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname

	cd bin
	install -Dm755 -t "$pkgdir"/usr/bin \
		victoria-metrics \
		vmagent \
		vmalert \
		vmauth \
		vmbackup \
		vmctl \
		vmrestore
}

_tools() {
	pkgdesc="$pkgdesc (vmagent, vmalert, vmauth, vmctl)"
	amove \
		usr/bin/vmagent \
		usr/bin/vmalert \
		usr/bin/vmauth \
		usr/bin/vmctl
}

_backup_tools() {
	pkgdesc="$pkgdesc (vmbackup, vmrestore)"
	amove \
		usr/bin/vmbackup \
		usr/bin/vmrestore
}

sha512sums="
556f7ae657a35956459f016d0388df5b3fb960cbf9b6de1011a01d8f61aa40c527bef8e69d13a185dc86305f29205a81eed314417b11eb30f8425b625e1a2fec  victoria-metrics-1.105.0.tar.gz
d727de5653e0ed9c7d7448dce6ab3766683d14e8d946935929691709c8b077572d5eb73c26749593cd7995820d370d46851be487fc03b663e495129c6a9e0244  victoria-metrics.initd
85bd8696eb701ad4a23063334a688a9915846558e3a57b0dbdf59c36c38cf435edcfdf66f8cb18611820b944ab529dedc0817b1f565050eeea2aec25ebda52ce  victoria-metrics.confd
"
